package com.edu.demo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@Configuration
public class JacksonConfig {
    public ObjectMapper objectMapper(){
        //设置key的序列化器
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //设置序列化时间格式
        ObjectMapper objectMapper = new ObjectMapper();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/shanghai"))));

        //设置反序列化时间格式
        javaTimeModule.addSerializer(
                LocalDateTime.class, new LocalDateTimeSerializer(
                        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/shanghai"))
                )
        );
        return objectMapper;
    }
}
